package hcontroller

import (
	"encoding/json"

	"advertoper/controllers"
	"advertoper/errorcode"
	"advertoper/homepageinfo/hservice"
)

type ZJSeckillInfoController struct {
	controllers.SCLoginedController
}

//秒杀数据查询，印证，已售数+1
type SeckillDataController struct {
	controllers.URLSignController
}

/*添加zjSeckill
{"region_code":"","clickable":1,"title":"空调挂机清洗9.9元",
"pic_url":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_picture":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_color":"#000000","starttick":1,"endtick":1499999999,"price":7990,"sell_number":11,
"routers":"zhjt://app.zjdj.com/webin?http://www.zhijiandaojia.com","total_number":99,
"seckill_start":1476341249,"seckill_end":1476427649,"share_url":"www.zhijianyaokong.com",
"share_logo":"","share_title":"分享","share_content":"分享内容","original_price":79}
*/
// @router /zjseckill/add [post]
func (p *ZJSeckillInfoController) ZJSeckillAdd() {
	defer p.Output("ZJSeckillAdd")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &hservice.ZJSeckillAddReq{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := hservice.GetZJSeckillInfoService().ZJSeckillAdd(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg

}

/*修改zjSeckill
{"id":1,"region_code":"","clickable":1,"title":"空调挂机清洗9.9元",
"pic_url":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_picture":"https://fscdn.qkier.com/scloud/e/b/c/d725bad2001dd6c76a189dd6e0f69",
"background_color":"#000000","starttick":1,"endtick":1499999999,"price":7990,"sell_number":11,
"routers":"zhjt://app.zjdj.com/webin?http://www.zhijiandaojia.com","total_number":99,
"seckill_start":1476341249,"seckill_end":1476427649,"share_url":"www.zhijianyaokong.com",
"share_logo":"","share_title":"分享","share_content":"分享内容","status":-1}
*/
// @router /zjseckill/update [post]
func (p *ZJSeckillInfoController) ZJSeckillUpdate() {
	defer p.Output("ZJSeckillUpdate")
	p.ForceHTTPS()

	body := p.Ctx.Input.RequestBody
	req := &hservice.ZJSeckillUpdateReq{}
	if err := json.Unmarshal(body, req); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	code, msg := hservice.GetZJSeckillInfoService().ZJSeckillUpdate(req)
	p.Resp["code"] = code
	p.Resp["msg"] = msg

}

//web查询
// @router /zjseckill/list [get]
func (p *ZJSeckillInfoController) ZJSeckillInfoList() {
	defer p.Output("ZJSeckillInfoList")
	p.ForceHTTPS()

	status, _ := p.GetInt("status", 1)
	data := hservice.GetZJSeckillInfoService().ZJSeckillInfoList(status)
	if len(data) == 0 {
		p.Resp["code"] = 101
		p.Resp["msg"] = "no data"
	} else {
		p.Resp["code"] = 0
		p.Resp["msg"] = "success"
		p.Resp["data"] = data
	}

}

//查询商品是否是秒杀,是秒杀，返回相应的基本信息(服务详情，商品详情用)
// @router /seckill/query [get]
func (p *SeckillDataController) QuerySeckillData() {
	defer p.Output("QuerySeckillData")
	p.ForceHTTPS()

	productType := p.GetString("seckill_product_type", "")
	productId, _ := p.GetInt("seckill_product_id", 0)
	regionCode := p.GetString("region_code", "")

	data := hservice.GetZJSeckillInfoService().QuerySeckillData(productType, regionCode, productId)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = data
}

//下单印证产品是否还在秒杀中，价格是否正确，数目是否还有,若正确，则商品已售数+1
// @router /seckill/verify [get]
func (p *SeckillDataController) VerifySeckillData() {
	defer p.Output("VerifySeckillData")
	p.ForceHTTPS()

	productType := p.GetString("seckill_product_type", "")
	productId, _ := p.GetInt("seckill_product_id", 0)
	productSid, _ := p.GetInt64("seckill_product_sid", 0)
	price, _ := p.GetInt("price", 0)
	buyNum, _ := p.GetInt("buy_num", 0)
	regionCode := p.GetString("region_code", "")

	code, msg, goodsId := hservice.GetZJSeckillInfoService().VerifySeckillData(productType, regionCode, productSid, productId, price, buyNum)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	p.Resp["seckill_id"] = goodsId
}

//取消订单时要释放商品数量
// @router /seckill/release [get]
func (p *SeckillDataController) ReleaseSeckillNum() {
	defer p.Output("ReleaseSeckillNum")
	p.ForceHTTPS()

	goodsId, _ := p.GetInt("seckill_id", 0)
	goodsNum, _ := p.GetInt("seckill_num", 0)
	goodsNum = 0 - goodsNum

	code, msg := hservice.GetZJSeckillInfoService().SeckillSellNumChange(goodsId, goodsNum)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
}
